package service.impl;

import common.impl.exception.ServiceException;

import model.MPassage;
import net.sf.json.JSONObject;
import service.interf.IPassage;
import dao.interf.IPassageDao;
import dao.pojo.Passage;



public class PassageImpl implements IPassage{
	private IPassageDao passageDao;
	
	public void setpassageDao(IPassageDao passageDao) {
		this.passageDao = passageDao;
	}
	
	@Override
	public String add(MPassage model) {
		Passage passage = new Passage();
	
		passage.setPassageContext(model.getPassageContext());
		
		return passageDao.add(passage);
	}
	
	@Override
	public String getById(MPassage model) {
	Passage passage = passageDao.getById(model.getPassageId());
	if(null == passage) {
		throw new ServiceException("ERR-0161");
	}
		
		return createPassageObj(passage).toString();
	}

	@Override
	public String deleteById(Integer id) {
		passageDao.deleteById(id);
		return "1";
	}

	@Override
	public String edit(MPassage model) {
		Passage passage = passageDao.getById(model.getPassageId());
		if (null == passage) {
			throw new ServiceException("ERR-0161");
		}

		passage.setPassageContext(model.getPassageContext());
		
		passageDao.edit(passage);
		return "1";
	}

	private JSONObject createPassageObj(Passage passage) {
		JSONObject obj = new JSONObject();
		obj.put("passageId", passage.getPassageId());
		obj.put("passageContext", passage.getPassageContext());
		
		return obj;							
	}

}
